package com.scisoft.scan.control.check;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.scisoft.scan.dy.R;
import com.scisoft.scan.data.check.CheckDetailBean;
import com.squareup.picasso.Picasso;

import java.util.List;

public class CheckDetailAdapter extends BaseAdapter {
    private List<CheckDetailBean> checkDetailBeanList;//数据源
    private LayoutInflater mInflater;//布局装载器对象
    private Context mContext;

    public CheckDetailAdapter(List<CheckDetailBean> checkDetailBeanList, Context context) {
        this.checkDetailBeanList = checkDetailBeanList;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        //return返回的是int类型，也就是页面要显示的数量。
        return checkDetailBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return checkDetailBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局文件转化为View对象
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_checkorderdetail, null);
        }

        //配件代码
        TextView tv_partNo = convertView.findViewById(R.id.tv_partNo);
        //配件名称
        TextView tv_partName = convertView.findViewById(R.id.tv_partName);
        //供应商代码
        TextView tv_supplierCode = convertView.findViewById(R.id.tv_supplierCode);
        //供应商
        TextView tv_supplierName = convertView.findViewById(R.id.tv_supplierName);
        //原厂件
        TextView tv_ymj = convertView.findViewById(R.id.tv_ymj);
        //仓库
        TextView tv_depotName = convertView.findViewById(R.id.tv_depotName);
        //库区
        TextView tv_areaName = convertView.findViewById(R.id.tv_areaName);
        //货位
        TextView tv_position = convertView.findViewById(R.id.tv_position);

        //实盘数量
        TextView tv_checkCount = convertView.findViewById(R.id.tv_checkCount);
        //占用数量
        TextView tv_occupyCount = convertView.findViewById(R.id.tv_occupyCount);
        //账面数量
        TextView tv_allCount = convertView.findViewById(R.id.tv_allCount);
        //状态
        TextView tv_state = convertView.findViewById(R.id.tv_state);
        //图片
        ImageView iv_img = convertView.findViewById(R.id.iv_img);

        //获取相应索引的ItemBean对象
        CheckDetailBean bean = checkDetailBeanList.get(position);

        //设置控件的对应属性值
        tv_partNo.setText(bean.partCode);
        tv_partName.setText(bean.partName);
        tv_supplierCode.setText(bean.manufacturer);
        tv_supplierName.setText(bean.manufacturerName);
        tv_depotName.setText(bean.depotName);
        tv_areaName.setText(bean.areaName);
        tv_position.setText(bean.position);

        tv_checkCount.setText(String.valueOf(bean.checkCount));
        tv_allCount.setText(String.valueOf(bean.allCount));
        tv_occupyCount.setText(bean.occupyCount);
        String state = "无";
        // 1 未盘 2 在盘 3盘盈 4盘平 5盘亏
        switch (bean.state) {
            case "1":
                state = "未盘";
                tv_state.setTextColor(Color.parseColor("#0287df"));
                tv_state.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_stat_blue));
                tv_state.setPadding(10, 2, 10, 2);
                break;
            case "2":
                state = "在盘";
                tv_state.setTextColor(Color.parseColor("#0287df"));
                tv_state.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_stat_blue));
                tv_state.setPadding(10, 2, 10, 2);
                break;
            case "3":
                state = "盘盈";
                tv_state.setTextColor(Color.parseColor("#FFFFFFFF"));
                tv_state.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_button_state_red));
                tv_state.setPadding(10, 2, 10, 2);
                break;
            case "4":
                state = "盘平";
                tv_state.setTextColor(Color.parseColor("#FFFFFFFF"));
                tv_state.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_button_state_blue));
                tv_state.setPadding(10, 2, 10, 2);
                break;
            case "5":
                state = "盘亏";
                tv_state.setTextColor(Color.parseColor("#FFFFFFFF"));
                tv_state.setBackground(mContext.getResources().getDrawable(R.drawable.edit_bg_button_state_green));
                tv_state.setPadding(10, 2, 10, 2);
                break;
        }
        tv_state.setText(state);
        if (bean.imgUrl != null && !bean.imgUrl.equals("")) {
            Picasso.with(mContext).load(bean.imgUrl).into(iv_img);
        } else {
            iv_img.setImageDrawable(convertView.getResources().getDrawable(R.drawable.ic_noimage));
        }
        String ymj = "";
        if ("0".equals(bean.ymj)) {
            ymj = "无码件";
        } else {
            ymj = "有码件";
        }
        tv_ymj.setText(ymj);
        return convertView;
    }
}
